
#import "SavingsCalculatorLampCalculation.h"

@interface SavingsCalculatorLampCalculation () {

    SavingsCalculator *_savingsCalculator;
}

@end


@implementation SavingsCalculatorLampCalculation

-(instancetype)initWithSavingsCalculator:(SavingsCalculator *)savingsCalculator {
    self = [super init];
    if (self) {
        _savingsCalculator = savingsCalculator;
    }
    return self;
}

-(CGFloat)lampsNeeded {
    return (CGFloat)_savingsCalculator.calculationHours / self.lampType.lifeExpectancy;
}

-(CGFloat)lampsPurchaseCost {
    return self.lampsNeeded * self.price;
}

-(CGFloat)usedEnergy {
    CGFloat const kWattPerKiloWatt = 1000;

    return self.effect / kWattPerKiloWatt * _savingsCalculator.calculationHours;
}

-(CGFloat)usedEnergyCost {
    return self.usedEnergy * _savingsCalculator.energyPriceKWh;
}

-(CGFloat)totalCost {
    return self.lampsPurchaseCost + self.usedEnergyCost;
}

-(CGFloat)totalSavingsExact {
    return _savingsCalculator.oldLampCalculation.totalCost - self.totalCost;
}

-(CGFloat)totalSavings {
    return self.totalSavingsExact < 0 ? 0 : roundf(self.totalSavingsExact / 5) * 5;
}

-(NSString *)description {
    NSMutableString *description = [NSMutableString string];

    [description appendFormat:@"** %@ **\n", self.lampType.name];
    [description appendFormat:@"Power need: %.2f\n", self.lampType.powerNeedRelativeClassic];
    [description appendFormat:@"Effect: %.2f W (calculated)\n", self.effect];
    [description appendFormat:@"Life expectancy: %ld h\n", (long)self.lampType.lifeExpectancy];
    [description appendFormat:@"Lamps needed: %.2f pcs\n", self.lampsNeeded];
    [description appendFormat:@"Lamps purchaseCost: %.2f kr\n", self.lampsPurchaseCost];
    [description appendFormat:@"Used energy and cost: %.2f kWh, %.2f kr\n", self.usedEnergy, self.usedEnergyCost];
    [description appendFormat:@"Total cost: %.2f kr\n", self.totalCost];
    [description appendFormat:@"Total savings: %.2f kr (%.2f kr exact)\n", self.totalSavings, self.totalSavingsExact];
    
    return description;
}

@end
